/*******************************************************************************

This code file produces Figure A19, "Do Differences in Neighborhood Characteristics Explain Cost Differences?"

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"

*** Compile housing sources
	
	*** Tenant-Based Section 8 

		import delimited "$data/raw/OtherHousingAssistance/Section8/hudPicture2015_360148.csv", encoding(ISO-8859-1) clear
		
		gen borough = substr(code,1,5)
		destring borough, replace force
		
		replace borough = 1 if borough == 36061
		replace borough = 2 if borough == 36005
		replace borough = 3 if borough == 36047
		replace borough = 4 if borough == 36081 
		replace borough = 5 if borough == 36085
		
		gen ct2010 = substr(code,6,6)
		destring ct2010, replace force
		
		drop if borough > 5
		
		rename reported assisted_unit_count
		rename averagehudexpenditurepermonth hudcost
		
		collapse (sum) assisted_unit_count (mean) hudcost, by(borough ct2010)
		
		merge 1:m borough ct2010 using "$data/clean/census_area_characteristics.dta", keep(2 3) nogen
		
		bys borough ct2010: egen sum_units = sum(ct_occ_renter_units*sh_poor)
		gen sh_units = (ct_occ_renter_units*sh_poor) / sum_units
		replace assisted_unit_count = assisted_unit_count * sh_units
		drop sum_units sh_units
		
		gen sh_mtcoll = sh_coll + sh_postgrad
		
		gcollapse (sum) assist (mean) $controls_block hudcost, by(borough ct2010 cb2010)
		
		rename assisted_unit_count assisted_unit_count_tbsection8
		
		tempfile assisted_tbsection8
		save `assisted_tbsection8', replace

	*** LIHTC

		* Obtain distribution of desired match variable
		
		use "$data/clean/non_421a_units.dta", clear
		keep if bldg == 2
				
		merge m:1 borough ct2010 cb2010 using "$data/clean/census_area_characteristics.dta", keep(2 3) nogen

		gen sh_mtcoll = sh_coll + sh_postgrad
		
		gcollapse (sum) assisted_unit_count (mean) $controls_block, by(borough ct2010 cb2010)
		
		merge 1:1 borough ct2010 cb2010 using `assisted_tbsection8', nogen
		replace assisted_unit_count = assisted_unit_count + assisted_unit_count_tbsection8
		drop assisted_unit_count_tbsection8
		
		tempfile assisted_tbsection8_lihtc
		save `assisted_tbsection8_lihtc', replace

	*** 421-a units	

		use "$data/clean/cleaned_data.dta", clear
					
		tempfile bldg_weights
		save `bldg_weights'
		
		merge m:1 borough ct2010 cb2010 using "$data/clean/census_area_characteristics.dta", keep(2 3) nogen
		
		gen sh_mtcoll = sh_coll + sh_postgrad
		
		gcollapse (sum) unitsres (mean) $controls_block if inclusionary_onsite == 1, by(borough ct2010 cb2010)
		
		tempfile units_421a
		save `units_421a', replace
		
*** Make graph

	*** Merge data together
	
		merge 1:1 borough ct2010 cb2010 using `assisted_tbsection8_lihtc', nogen
		
	*** Split Census blocks by income decile
	
		egen inc_decile = cut(lmedhhinc), group(10)
		
	*** Create decile shares of units
	
		collapse (sum) unitsres assisted_unit_count, by(inc_decile)
	
		summ unitsres
		local tot = r(sum)
		
		gen sh_421a = unitsres / `tot'
		
		summ assisted_unit_count
		local tot = r(sum)
		
		gen sh_s8lihtc = assisted_unit_count / `tot'
		
		graph bar sh_421a sh_s8lihtc, over(inc_decile) b1title("Income Decile of Census Block Group") ///
			legend(order(1 "421-a Units" 2 "Section 8 and LIHTC Units")) ///
			graphregion(color(white)) plotregion(margin(zero)) ///
			ylabel(, nogrid) ytitle("Share of Units")
		
	graph export "$figs/distribution_421a_vs_Section8LIHTC.pdf", replace
	graph export "$figs_overleaf/distribution_421a_vs_Section8LIHTC.pdf", replace
		
